﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

//@author A0085749L
namespace EZXPNS
{
    static class EzXpns
    {
        //To store income items
        public static List<Item> _incomeList;
        
        //To store expense items
        public static List<Item> _expenseList;

        //To store undo information
        public static Stack<Item> _undoList;
        public static Stack<int> _undoIndex;
        public static Stack<string> _commands;
        
        //Constructor
        static EzXpns()
        {
            _incomeList=new List<Item>();
            _expenseList=new List<Item>();

            _undoList = new Stack<Item>();
            _undoIndex = new Stack<int>();
            _commands = new Stack<string>(); 

        }

        //Write working memory data to data storage-IncomeList.txt file
        public static void SaveIncome()
        {
            StreamWriter outputfile;
            outputfile = File.CreateText("../../IncomeList.txt");

            for (int i = 0; i < _incomeList.Count; i++)
            {
                outputfile.WriteLine(_incomeList[i].Name);
                outputfile.WriteLine(_incomeList[i].Amount.ToString());
                outputfile.WriteLine(_incomeList[i].Category);
                outputfile.WriteLine(_incomeList[i].Date);
                outputfile.WriteLine(_incomeList[i].Payment);
            }

            outputfile.Close();    
        }

        //Write working memory data to data storage-ExpenseList.txt file
        public static void SaveExpense()
        {
            StreamWriter outputfile;
            outputfile = File.CreateText("../../ExpenseList.txt");

            for (int i = 0; i < _expenseList.Count; i++)
            {
                outputfile.WriteLine(_expenseList[i].Name);
                outputfile.WriteLine(_expenseList[i].Amount.ToString());
                outputfile.WriteLine(_expenseList[i].Category);
                outputfile.WriteLine(_expenseList[i].Date);
                outputfile.WriteLine(_expenseList[i].Payment);
            }

            outputfile.Close();

        }
        
        //To return alert information
        public static string Alert()
        {
            string alert = "" ;

            decimal totalIncome = 0m;
            for (int i = 0; i < EzXpns._incomeList.Count; i++)
            {
                totalIncome += EzXpns._incomeList[i].Amount;
            }

            decimal totalExpense = 0m;
            for (int i = 0; i < EzXpns._expenseList.Count; i++)
            {
                totalExpense += EzXpns._expenseList[i].Amount;
            }

            StreamReader inputfile;
            inputfile = File.OpenText("../../GoalSaving.txt");
            decimal goal = decimal.Parse(inputfile.ReadLine());
            inputfile.Close();

            if (totalExpense > totalIncome)
                alert = "You are spending too much!";
            else if ((totalIncome - totalExpense) >= goal) 
                alert = "Congratulations! You have reached the goal!";
            else if ((totalIncome - totalExpense) > goal * 0.9m)
                alert = "You are 90% to the goal!";
            else if ((totalIncome - totalExpense) > goal * 0.8m)
                alert = "You are 80% to the goal!";
            else if ((totalIncome - totalExpense) > goal * 0.7m)
                alert = "You are 70% to the goal!";
            else if ((totalIncome - totalExpense) > goal * 0.6m)
                alert = "You are 60% to the goal!";
            else if ((totalIncome - totalExpense) > goal * 0.5m)
                alert = "You are halfway to the goal!";
            else if ((totalIncome - totalExpense) > goal * 0.4m)
                alert = "You are 40% to the goal!";
            else if ((totalIncome - totalExpense) > goal * 0.3m)
                alert = "You are 30% to the goal!";
            else if ((totalIncome - totalExpense) > goal * 0.2m)
                alert = "You are 20% to the goal!";
            else if ((totalIncome - totalExpense) > goal * 0.1m)
                alert = "You are 10% to the goal!";
            else
                alert = "You are below 10% to the goal!";
            return alert;
        }
    }
}
